<?php

namespace App\Http\Controllers;

use App\Cate;
use App\Norm;
use Illuminate\Http\Request;

class NormController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $where = [];
        if(!empty($request->start)){
            $where[] = ['created_at',">",$request->start];
        }
        if(!empty($request->end)){
            $where[] = ['created_at',"<",$request->end];
        }
        if(!empty($request->name)){
            $where[] = ['name',$request->name];
        }
        $norms = Norm::where($where)->paginate(2);

        return view("admin.norm.index",compact("norms", "request"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $cates = Cate::all();
        return view("admin.norm.create",compact("cates"));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Norm $norm, Request $request)
    {
        //
        $this->validate($request,[
            "name"=>"required|unique:cates|max:10|min:1",
            "cate_id"=>"required",
        ],
            [
                "name.unique"=>"分类名称不能重复",
                "name.required"=>"分类名称必须填写",
                "name.max"=>"分类名称过长",
                "name.min"=>"分类名称过短",
                "cate_id.required"=>"没有选择分类",
            ]
        );
        $norm->name = $request->name;
        $norm->cate_id = $request->cate_id;
        $norm->save();
        return back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Norm $norm)
    {
        //
        $cates = Cate::all();
        return view("admin.norm.edit", compact("norm","cates"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Norm $norm)
    {
        //
        $norm->name = $request->name;
        $norm->cate_id = $request->cate_id;
        $norm->value = $request->value;
        $norm->save();
        return back();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Norm $norm)
    {
        //
        $norm->delete();
        return back();
    }

    public function destroyAll(Request $request)
    {
        $res = Cate::destroy($request->data);
        if ($res){
            return [
                "error"=>0,
            ];
        }
    }
}
